Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var showType = 1;
	var articleTree = new Ext.tree.TreePanel({
		title:'文章类别',
		region:'west',
		frame:true,
		width:200,
		split:true,
		collapsible:true,
		rootVisible : false,
		autoScroll : true,
		root:new Ext.tree.AsyncTreeNode({
			id : 'root',
			text : '类别'
		}),
		loader: new Ext.tree.TreeLoader({
			url:'/wsd/admin/data/article_command_tree.json'
    	})
	});
	articleTree.expandAll();
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([
		sm,
		{header:'文章编号',dataIndex:'AId'},
		{header:'文章类型',dataIndex:'AModel',renderer:function(value){
			switch(value){
				case 1:
					return '公司新闻';
				case 2:
					return '行业资讯'
			}
			
		}},
		{header:'文章标题',dataIndex:'ATitle'},
		{header:'发布时间',dataIndex:'postTime'},
		{header:'作者',dataIndex:'adminName'}
	])
	
	var articleStore = new Ext.data.JsonStore({
		url:'/wsd/admin/article_getArticles.action',
		totalProperty:'total',
		root:'result.articles',
		autoLoad:true,
		baseParams:{
			start:0,
			limit:10,
			'article.AModel':null
		},
		fields:['AId','AModel','ATitle','postTime','AContent',
				{name:'adminName',mapping:'admin.adminName'}
			   ]
	})
	
	var articlePanel = new Ext.grid.GridPanel({
		region:'center',
		store:articleStore,
		viewConfig:{
			forceFit:true
		},
		loadMask:true,
		autoScroll:true,
		cm:cm,
		sm:sm,
		viewConfig:{
			forceFit:true
		},
		bbar:new Ext.PagingToolbar({
			pageSize:10,
			store:articleStore,
			displayInfo:true,
			displayMsg:'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			emptyMsg:'没有记录'
		}),
		tbar:new Ext.Toolbar([
			{
				text:'查看修改',
				handler:function(){
					var records = articlePanel.getSelectionModel().getSelections();
					//updateState(records,true);
					createUpdateWindow(records[0].data);
					
				}
			},
			'->',
			{
				text:'删除',
				handler:function(){
					var records = sm.getSelections();
					del(records);
				}
			
			}
		])
	})
	
	articlePanel.on('rowdblclick',function(grid,rowIndex,e){
		//openOrderListWindow()
		var article = articlePanel.getStore().getRange()[rowIndex].data;
		createUpdateWindow(article);
		
	})
	//中间主面板
	var panel = new Ext.Panel({
		region:'center',
		layout:'border',
		items:[articlePanel]
	})
	
	
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [panel,articleTree]
	});
	
	articleTree.on("click",function(node,e){
		var model = node.attributes.id == 0?null:node.attributes.id;
		articleStore.baseParams['article.AModel'] = model;
		articleStore.load({
			params:{
				start:0,
				limit:10
			}
		})
	})
	
	function del(records){
		var len = records.length;
		if(len == 0){
			Ext.Msg.alert('系统信息','请先选择文章');
			return;
		}
		var ids = new Array();
		for(var i = 0; i < len; i++){
			ids.push(records[i].data.AId);
		}
		Ext.Msg.wait('正在删除，请稍后……');
		Ext.Ajax.request({
			url:'/wsd/admin/article_delete.action',
			params:{
				ids:ids
			},
			success:function(response){
				Ext.Msg.hide();
				articleStore.reload();
				Ext.Msg.alert('系统信息','删除成功!');
			},
			failure:function(response){
				Ext.Msg.hide();
				Ext.Msg.alert('系统信息','删除成功!');
			}
		})
	}
	function createUpdateWindow(article){
		var articleIdField = new Ext.form.Hidden({
			name:'article.AId',
			value:article.AId
		})
		var titleField = new Ext.form.TextField({
			fieldLabel:'标题',
			width:400,
			emptyText:'请输入文章标题',
			allowBlank:false,
			blankText:'文章标题不能为空',
			name:'article.ATitle',
			value:article.ATitle,
			msgTarget:'under'
		})
		var typeCombo = new Ext.form.ComboBox({
			fieldLabel:'文章类别',
			mode:'local',
			name:'text',
			triggerAction:'all',
			valueField:'article.AModel',
			value:article.AModel,
			displayField:'AModel',
			//value:1,
			editable:false,
			store:new Ext.data.SimpleStore({
				fields:['article.AModel','AModel'],
				data:[
					['1','公司新闻'],
					['2','行业资讯']
				]
			})
		})
		var contentField = new Ext.form.HtmlEditor({
			fieldLabel:'内容',
			widt:800,
			height:300,
			name:'article.AContent',
			value:article.AContent,
			autoScroll:true
		})
		var postBtn = new Ext.Button({
			text:'修改',
			handler:function(){
				if(!articleForm.getForm().isValid()){
					return;
				}
				articleForm.getForm().submit({
					url:'admin/article_update.action',
					params:{
						'article.AModel':typeCombo.getValue()
					},
					waitMsg: '正在修改，请稍后……',
					success: function(fp, o){
						articleStore.reload();
			       		Ext.Msg.alert('系统信息',"修改成功!");
		            },
		            failure:function(fp, o){
		            	Ext.Msg.alert('系统信息',o.result.result.msg);
		            }
				})
			}
		})
		var exitBtn = new Ext.Button({
			text:'退出',
			handler:function(){
				articleWindow.close();
			}
		})
		//中间主面板
		var articleForm = new Ext.FormPanel({
			region:'center',
			frame:true,
			autoScroll:true,
			items:[titleField,articleIdField,typeCombo,contentField],
			buttons:[postBtn,exitBtn]
		})
		
		var articleWindow = new Ext.Window({
			layout:'border',
			title:'文章修改',
			closeAction:'hide',
			modal:true,
			height:400,
			width:800,
			maximizable:true,
			constrain:true,//防止窗口超出边界,
			items:[articleForm]
		})
		articleWindow.show();
		
	}
});